﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Touch2Like.Model;
using Touch2Like.SharedLibrary;
using System.Data;
using System.Data.SqlClient;

namespace Touch2Like.DAL
{
    public class UserTagDAL
    {
        public int DeleteUserTag(int UserTagID)
        {
            Database db = new Database();
            SqlParameter[] param =
            {
                db.MakeInParam("@UserTagID",SqlDbType.Int,4, UserTagID)
            };
            try
            {
                db.ExecNonQuery("CAM_UserTag_Delete", param);
                return 1;
            }
            catch (Exception ex)
            {
                return 0;
            }
            finally
            {
                db.Close();
            }
        }
        public UserTagInfo GetUserTagByTagUID(string TagUID)
        {
            Database db = new Database();
            SqlDataReader dr = null;
            SqlParameter[] param =
            {
                db.MakeInParam("@TagUID",SqlDbType.VarChar,50,TagUID)
            };
            try
            {
                dr = db.RunProc("CAM_UserTag_GetByTagUID", param);
                if (dr.Read())
                {
                    UserTagInfo info = new UserTagInfo();
                    info.Alias = DataHelper.GetString(dr["Alias"]);
                    info.TagContent = DataHelper.GetString(dr["TagContent"]);
                    info.Void = DataHelper.GetInt(dr["Void"]);
                    info.TagUID = DataHelper.GetString(dr["TagUID"]);
                    info.UserGUID = DataHelper.GetString(dr["UserGUID"]);
                    info.UserTagID = DataHelper.GetInt32(dr["UserTagID"]);
                    info.TagType = DataHelper.GetString(dr["TagType"]);
                    return info;
                }
                return null;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dr.Close();
                db.Close();
            }
        }
        public DataTable GetUserTagByUserGUID(string UserGUID)
        {
            Database db = new Database();
            DataSet ds = new DataSet();
            SqlParameter[] param =
            {
                db.MakeInParam("@UserGuid",SqlDbType.UniqueIdentifier,16,new Guid(UserGUID))
            };
            try
            {
                if (UserGUID == null)
                {
                    return null;
                }
                else
                {
                    ds = db.GetDataSet("CAM_UserTag_GetByUserGUID", param);
                    if (ds != null)
                        return ds.Tables[0];
                    else
                        return null;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                db.Close();
            }
        }
        public UserTagInfo GetUserTagByUserTagID(int UserTagID)
        {
            Database db = new Database();
            SqlDataReader dr = null;
            SqlParameter[] param =
            {
                db.MakeInParam("@UserTagID",SqlDbType.Int,4,UserTagID)
            };
            try
            {
                dr = db.RunProc("CAM_UserTag_GetByUserTagID", param);
                if (dr.Read())
                {
                    UserTagInfo info = new UserTagInfo();
                    info.Alias = DataHelper.GetString(dr["Alias"]);
                    info.TagContent = DataHelper.GetString(dr["TagContent"]);
                    info.Void = DataHelper.GetInt(dr["Void"]);
                    info.TagUID = DataHelper.GetString(dr["TagUID"]);
                    info.UserGUID = DataHelper.GetString(dr["UserGUID"]);
                    info.UserTagID = DataHelper.GetInt32(dr["UserTagID"]);
                    info.TagType = DataHelper.GetString(dr["TagType"]);
                    return info;
                }
                return null;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dr.Close();
                db.Close();
            }

        }
        public int AddUserTag(UserTagInfo info)
        {
            Database db = new Database();
            SqlParameter[] param =
            {
                db.MakeInParam("@UserGUID",SqlDbType.UniqueIdentifier,16,new Guid(info.UserGUID)),                
                db.MakeInParam("@TagUID",SqlDbType.VarChar,50,info.TagUID),
                db.MakeInParam("@TagType",SqlDbType.VarChar,20,info.TagType),
                db.MakeInParam("@Alias",SqlDbType.VarChar,50,info.Alias),
                db.MakeInParam("@TagContent",SqlDbType.VarChar,5000,info.TagContent),
                db.MakeInParam("@Void",SqlDbType.SmallInt,2,info.Void)
             };
            try
            {
                db.ExecNonQuery("CAM_UserTag_Add", param);
                return 1;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                db.Close();
            }
        }
        public int UpdateUserTag(UserTagInfo info)
        {
            Database db = new Database();
            SqlParameter[] param =
            {
                db.MakeInParam("@UserTagID",SqlDbType.Int,32,info.UserTagID),
                db.MakeInParam("@UserGUID",SqlDbType.UniqueIdentifier,16,new Guid(info.UserGUID)),                
                db.MakeInParam("@TagUID",SqlDbType.VarChar,50,info.TagUID),
                db.MakeInParam("@TagType",SqlDbType.VarChar,20,info.TagType),
                db.MakeInParam("@Alias",SqlDbType.VarChar,50,info.Alias),
                db.MakeInParam("@TagContent",SqlDbType.VarChar,5000,info.TagContent),
                db.MakeInParam("@Void",SqlDbType.SmallInt,2,info.Void)
            };
            try
            {
                db.ExecNonQuery("CAM_UserTag_Update", param);
                return 1;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                db.Close();
            }

        }
    }
}
